<?php

namespace services\patient;

use Yii;
use common\components\Service;
use common\models\patient\AllocationRecord;

/**
 * Class AllocationRecordService
 * @package services\patient
 */
class AllocationRecordService extends Service
{
    /**
     * 根据ID查找信息
     *
     * @param int $id
     * @return AllocationRecord|null
     */
    public function findById(int $id = 0): ?AllocationRecord
    {
        return AllocationRecord::findOne($id);
    }

    /**
     * 查找信息
     *
     * @param array $condition
     * @return AllocationRecord|null
     */
    public function findInfo(array $condition = []): ?AllocationRecord
    {
        $query = AllocationRecord::find();

//        isset($condition['patient_user_id']) && $query->andWhere(['patient_user_id' => $condition['patient_user_id']]);

        return $query->one();
    }

    /**
     * 查找列表
     *
     * @param array $condition
     * @return array|\yii\db\ActiveRecord[]
     */
    public function findList(array $condition = [])
    {
        $query = AllocationRecord::find();

        // select
        isset($condition['select']) && $query->select($condition['select']);

        // condition
        isset($condition['id']) && $query->andWhere(['id' => $condition['id']]);
        isset($condition['project_id']) && $query->andWhere(['project_id' => $condition['project_id']]);
        isset($condition['member_id']) && $query->andWhere(['member_id' => $condition['member_id']]);
        if (isset($condition['start_time']) && $condition['start_time']) {
            $query->andWhere(['>=', 'created_at', strtotime($condition['start_time'])]);
        }
        if (isset($condition['end_time']) && $condition['end_time']) {
            $query->andWhere(['<=', 'created_at', strtotime($condition['end_time'].' 23:59:59')]);
        }

        // indexBy
        isset($condition['indexBy']) && $query->indexBy($condition['indexBy']);

        return $query->asArray()->all();
    }
}
